/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services.ie.com.stamply.security


import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Fabiano
 */
class StamplyAuthSuccessHandler  extends SavedRequestAwareAuthenticationSuccessHandler {
    
    private String userUrl;
    private String adminUrl;
     
    public void setUserUrl(String userUrl){
        this.userUrl = userUrl;
    }
     
    public void setAdminUrl(String adminUrl){
        this.adminUrl = adminUrl;
    }
     
    
    @Override
    protected String determineTargetUrl(HttpServletRequest request,
                                        HttpServletResponse response) {
         
        boolean hasAdmin = SpringSecurityUtils.ifAllGranted("ROLE_ADMIN");
        boolean hasUser = SpringSecurityUtils.ifAllGranted("ROLE_CUSTOMER");
         
        if(hasAdmin){
            return adminUrl;
        }else if (hasUser){
            return userUrl;
        }else{
            return super.determineTargetUrl(request, response);
        }
    }
	
}

